import 'package:app_settings/app_settings.dart';
import 'package:auto_route/auto_route.dart';
import 'package:casdon/app/config/theme/theme_extention.dart';
import 'package:casdon/build_gen/assets.gen.dart';
import 'package:casdon/common/widget/Info_notification_bar.dart';
import 'package:casdon/common/widget/badge_icon.dart';
import 'package:casdon/common/widget/common_app_bar.dart';
import 'package:casdon/common/widget/common_text_button.dart';
import 'package:casdon/common/widget/fullscreen_loading_overlay.dart';
import 'package:casdon/core/utils/wifi/wifi_status_monitor.dart';

import 'package:casdon/modules/device/ui/widgets/product_category_widget.dart';
import 'package:casdon/modules/device/ui/widgets/search_device_status.dart';
import 'package:casdon/modules/device/UI/widgets/device_discoveried_list_widget.dart';

import 'package:casdon/modules/device/ui/widgets/ble_not_open_widget.dart';
import 'package:casdon/core/utils/blue_tooth/ble_status_monitor.dart';
import 'package:casdon/modules/device/application/notification/product_category_notifier.dart';
import 'package:casdon/auto_router.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

@RoutePage()
class DeviceHomeScreen extends ConsumerWidget {
  const DeviceHomeScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final bleToggleState = ref.watch(bluetoothStatusProvider);
    final wifiToggleState = ref.watch(wifiStatusProvider);
    final productCategoryState = ref.watch(productCategoryNotifierProvider);

    return Scaffold(
      appBar: CommonAppBar(
        title: '添加设备',
        rightActions: [
          BadgeIcon(
            imagePath: Assets.images.icScanner.path,
            onPressed: () {
              _goScannerScreen(context);
            },
          ),
        ],
      ),
      body: Stack(
        children: [
          Column(
            children: [
              wifiToggleState.when(
                data: (state) {
                  if (state == false) {
                    return InfoNotificationBar(
                      message: "开启WLAN，以查找到更多设备",
                      rightButton: CommonTextButton(
                        text: "去开启",
                        isTrueState: true,
                        onPressedWithState: (bool isTrueState) {
                          _openWLANSetting(context);
                        },
                      ),
                    );
                  } else {
                    return const SizedBox.shrink();
                  }
                },
                loading: () => const SizedBox.shrink(),
                error: (err, _) => const SizedBox.shrink(),
              ),

              bleToggleState.when(
                data: (state) {
                  if (state == BluetoothAdapterState.on) {
                    return SearchDeviceStatus();
                  } else {
                    return const BLENotOpenWidget();
                  }
                },
                loading: () => const SizedBox.shrink(),
                error: (err, _) => const SizedBox.shrink(),
              ),
              const DeviceDiscoveriedListWidget(),
              const Divider(height: 1, thickness: 1, color: Color(0xFFF3F3F3)),
              const Expanded(child: ProductCategoryWidget()),
            ],
          ),
        ],
      ),
    );
  }

  // 前往扫码
  void _goScannerScreen(BuildContext context) async {
    Future.microtask(() {
      if (context.mounted) {
        QRScannerRoute().push(context);
      }
    });
  }

  // 开启WLAN设置
  void _openWLANSetting(BuildContext context) {
    AppSettings.openAppSettings(type: AppSettingsType.wifi);
  }
}
